package com.checkoutcheckins.client.widgets;

import com.checkoutcheckins.client.data.Utils;
import com.checkoutcheckins.client.data.foursquare.FoursquareUser;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Image;

public class UserWidget extends FlexTable {

	private FoursquareUser fsUser;
	
	public UserWidget(FoursquareUser fsUser, boolean showLastSeen) {
		super();
		this.fsUser = fsUser;
		init();
		if (showLastSeen) {
			this.showLastSeen();
		}
		this.addSignoutButton();
	}

	private void addSignoutButton() {
		
		Button signout = new Button("Sign out");
		signout.setStyleName("button");
		signout.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				Cookies.removeCookie("4sqtoken");
				Cookies.removeCookie("4sqsecret");
				Window.open("/", "_self", "");
			}
		});

		this.setWidget(4, 0, signout);
		this.getFlexCellFormatter().setColSpan(4, 0, 2);
		this.getFlexCellFormatter().setHorizontalAlignment(4, 0, HasHorizontalAlignment.ALIGN_CENTER);		
	}

	private void init() {
		this.setWidget(0, 0, new Image(fsUser.getPhoto()));
		this.getFlexCellFormatter().setHorizontalAlignment(0, 0,
				HasHorizontalAlignment.ALIGN_RIGHT);

		Anchor userName = new Anchor(fsUser.getFirstname() + " "
				+ fsUser.getLastname());
		userName.setHref("http://www.foursquare.com/user/-" + fsUser.getId());
		userName.setStyleName("onefifty");

		this.setWidget(0, 1, userName);
		this.getFlexCellFormatter().setRowSpan(0, 0, 2);
		this.setStyleName("userWidget");
	}

	public void showLastSeen() {
		
		StringBuffer lastSeen = new StringBuffer();
		lastSeen.append("Last seen <span style='font-size: 150%'>");
		lastSeen.append(Utils.humanFriendlyDate(fsUser.getCheckin().getCreatedDate()));
		lastSeen.append("</span>");

		
		if (fsUser.getCheckin().getVenue() != null)
		{
			lastSeen.append(" visiting <span style='font-size: 150%'>");
			lastSeen.append(fsUser.getCheckin().getVenue().getName());
			lastSeen.append("</span>");

		}
		if (fsUser.getCheckin().getShout() != null)
		{
			lastSeen.append(" shouting <span style='font-size: 150%'>");
			lastSeen.append("\"" + fsUser.getCheckin().getShout() + "\"");
			lastSeen.append("</span>");

		}
		
		HTML lastSeenHTML = new HTML(lastSeen.toString());
		lastSeenHTML.setStyleName("seventy");

		this.setWidget(3, 0, lastSeenHTML);
		this.getFlexCellFormatter().setColSpan(3, 0, 2);
		this.getFlexCellFormatter().setHorizontalAlignment(3, 0, HasHorizontalAlignment.ALIGN_CENTER);

	}

}
